package top.smallway.Myiews;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Dialog extends JFrame {
    private boolean isDraging;
    private int xx, yy;

    public Dialog() {
        setSize(700, 200);
        setUndecorated(true);
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();  //获取屏幕对象
        setBackground(new Color(0, 0, 0, 0));
        int x = (int) screensize.getWidth() / 2 - getWidth() / 2;
        int y = (int) screensize.getHeight() / 2 - getHeight() / 2;
        setLocation(x, y);
        setLocationRelativeTo(null);  //自动居中对齐
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                isDraging = true;
                xx = e.getX();
                yy = e.getY();
            }


            @Override
            public void mouseReleased(MouseEvent e) {
                isDraging = false;
            }
        });
        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isDraging) {
                    int left = getX();
                    int top = getY();
                    setLocation(left + e.getX() - xx, top + e.getY() - yy);
                }
            }
        });
        Panels total = new Panels();
        total.setOpaque(false);
        this.add(total);
    }

    static class Panels extends JPanel {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            BufferedImage backgroundImage;
            try {
                backgroundImage = ImageIO.read(new File("src/main/resources/Dialog_bj.png"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            // 确保 JPanel 背景是透明的
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 绘制 PNG 图像 (确保透明部分显示)
            if (backgroundImage != null) {
                g2d.drawImage(backgroundImage, 0, 0, null);
            }
        }
    }
}
